#include "attr_node.h"

#include "node.h"
#include "namespace.h"
#include "attribute.h"
#include "element.h"

#include <string>
#include <list>
#include <iostream>

using namespace std;

AttrNode::AttrNode() {
	mAttributes = new list<XmlAttribute*>;
}

AttrNode::AttrNode(const AttrNode& e) {
	if(e.mAttributes != NULL)
	{
		for (list<XmlAttribute*>::const_iterator iter=(e.mAttributes->begin());
			 iter!=(e.mAttributes->end());
			 iter++) {
		
			mAttributes->push_back(new XmlAttribute(*(*iter)));
		}
	}
	this->mTag = e.mTag;
	this->mNamespace = e.mNamespace;
}

AttrNode::AttrNode(std::string tag) : mTag(tag) {
	mAttributes = new list<XmlAttribute*>;
}

AttrNode::AttrNode(std::string tag, std::string ns) : mTag(tag), mNamespace(ns) {
	mAttributes = new list<XmlAttribute*>;
}

AttrNode::~AttrNode() {
	for (list<XmlAttribute*>::iterator iter=(mAttributes->begin());
		 iter!=(mAttributes->end());
		 iter++) {
		
		delete (*iter);
	}
	delete mAttributes;
}
	
void AttrNode::setAttributes(std::list<XmlAttribute*> *attrs ) {
	delete mAttributes;
	mAttributes = attrs;
}

void AttrNode::setTag(string& tag) {
	mTag.assign(tag);
}
void AttrNode::setNamespace(Namespace ns) {
	mNamespace = ns;
}

list<XmlAttribute*>* AttrNode::getAttributes() {
	return mAttributes;
}

string AttrNode::getAttributeValue(string name) {
	string value = string("");

	list<XmlAttribute*>::const_iterator iterator;

	for (iterator = mAttributes->begin(); iterator != mAttributes->end(); ++iterator) {
		if((*iterator)->getKey() == name)
			value = (*iterator)->getValue();
	}

	return value;
}

string AttrNode::getTag() {
	return mTag;
}

string AttrNode::getNSTag() {
	if (!mNamespace.empty()) {
		return mNamespace+":"+mTag;
	} else {
		return mTag;
	}
}

Namespace AttrNode::getNamespace () {
	return mNamespace;
}

void AttrNode::printAttributes(std::ostream& out) {


	if (mAttributes == NULL)
		return;


	for (list<XmlAttribute*>::iterator iter=(mAttributes->begin());
		 iter!=(mAttributes->end());
		 iter++) {
		
		(*iter)->print(out);
	}
}
